package com.keur.money.utils.helper

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.keur.money.base.BaseActivity
import com.keur.money.data.analyse.util.ReviewUtil
import com.keur.money.app.MyApp
import com.keur.money.base.utils.log.MullaLog
import com.keur.money.http.HttpHelper
import com.keur.money.ui.user.act.KeurUserFeedbackAct
import com.keur.money.utils.dialog.CommentNormalDialog
import com.keur.money.utils.event.KeurEventType
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/20 16:26
 * @Annotation：评分帮助类
 */
object KeurCommentDialogHelper {

    /**
     * 展示评分
     */
    fun showStarDialog(context: Context, data: JSONObject, isNeedAdd: Boolean){

        if (data.has("activeWindow") && data.optJSONObject("activeWindow") != null){
            var isShowActiveDialog = false
            val activeWindowObj = data.optJSONObject("activeWindow")
            activeWindowObj?.let {
                //需要弹出
                if (it.optString("switch") == "YES"){
                    if (isNeedAdd){//是否需要判断出现次数
                        val totalNum = it.optInt("num")
                        val curtNum = SharedHelper.getKeurSPInt(SharedHelper.KEUR_SPKEY.KEUR_DIALOG_ACTIVE_NUM)
                        if (curtNum < totalNum){
                            isShowActiveDialog = true
                            SharedHelper.saveKeurSPData(SharedHelper.KEUR_SPKEY.KEUR_DIALOG_ACTIVE_NUM,curtNum+1)
                        }
                    } else {
                        isShowActiveDialog = true
                    }
                }
                if (isShowActiveDialog){
                    showActiveStarDialog(context,it.optString("content"))
                    return
                }
            }
        }

        if (data.has("normalWindow") && data.optJSONObject("normalWindow") != null){
            val normalWindowObj = data.optJSONObject("normalWindow")
            normalWindowObj?.let {
                if (isNeedAdd){
                    if (it.optString("switch") == "YES"){
                        val totalNum = it.optInt("num")
                        val curtNum = SharedHelper.getKeurSPInt(SharedHelper.KEUR_SPKEY.KEUR_DIALOG_NORMAL_NUM)
                        if (curtNum >= totalNum){
                            return
                        }
                        SharedHelper.saveKeurSPData(SharedHelper.KEUR_SPKEY.KEUR_DIALOG_NORMAL_NUM,curtNum+1)
                        showNormalStarDialog(context,it.optString("content"))
                    }
                } else {//个人中心
                    showNormalStarDialog(context,it.optString("content"))
                }
            }
        }
    }

    /**
     * activeWindow
     */
    private fun showActiveStarDialog(context: Context, tips: String){
        val activeDialog = CommentNormalDialog(context,object : IStarDialogCallback{
            override fun completeScoringCount(count: Int) {
                MullaLog.e(">>>>>>>>>>score num：$count")
                HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_USER_RATE_US_RATE,count.toString())
                /**
                 * 数量 >= 4  ->  跳转Google 评分 （AppReviewUtil.launchReviewFlow() ）
                 * 数量 < 4   ->  跳转意见反馈  ( UserFeedbackAct )
                 */
                if (count < 4){
                    KeurUserFeedbackAct.start(context,"$count")
                } else {
                    val act = context as BaseActivity
                    ReviewUtil.launchReviewFlow(act, completed = {
                        MullaLog.e(">>>>>completed>>>>>>>>>>$it")
                        if (!it){
                            //跳转到应用市场
                            enterMarket()
                        }
                    })
                }
            }
        })
        activeDialog.setCancelable(true)
        activeDialog.setCanceledOnTouchOutside(false)
        activeDialog.show()
        activeDialog.setSibTextTips(tips)
    }

    /**
     * normalWindow
     */
    private fun showNormalStarDialog(context: Context, tips: String){
        val normalDialog = CommentNormalDialog(context,object : IStarDialogCallback{
            override fun completeScoringCount(count: Int) {
                MullaLog.e(">>>>>>>>>>score num：$count")
                HttpHelper.addKeurEventAction(KeurEventType.CLICK.KEUR_CLICK_USER_RATE_US_RATE,count.toString())
                /**
                 * 数量 >= 4  ->  跳转Google 评分 （AppReviewUtil.launchReviewFlow() ）
                 * 数量 < 4   ->  跳转意见反馈  ( UserFeedbackAct )
                 */
                if (count < 4){
                    KeurUserFeedbackAct.start(context,"$count")
                } else {
                    val act = context as BaseActivity
                    ReviewUtil.launchReviewFlow(act, completed = {
                        MullaLog.e(">>>>>completed>>>>>>>>>>$it")
                        if (!it){
                            //跳转到应用市场
                            enterMarket()
                        }
                    })
                }
            }
        })
        normalDialog.setCancelable(true)
        normalDialog.setCanceledOnTouchOutside(false)
        normalDialog.show()
        normalDialog.setSibTextTips(tips)
    }

    /**
     * 跳转到应用商店
     */
    private fun enterMarket(){
        val intent = Intent(Intent.ACTION_VIEW)
        intent.data = Uri.parse("https://play.google.com/store/apps/details?id=${MyApp.app.packageName}")
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        MyApp.app.startActivity(intent)
    }

    interface IStarDialogCallback {
        //返回评星数量
        fun completeScoringCount(count: Int)
    }
}